<?php


namespace app\workerman;
use GatewayWorker\Lib\Gateway;

class Events
{
    #客户状态相关
    const TYPE_CUSTOMER = 1; //客户类型
    const TYPE_SERVICER = 2; //客服类型
    const ONLINE_STATUS = 1; //在线状态
    const OFFLINE_STATUS = 0; //不在在线状态

    #TCP连接相关
    const CONNECT_TCP_STATUS_SUCCESS = 100; //TCP连接成功

    #websocket连接相关
    const CONNECT_WEBSOCKET_STATUS_SUCCESS = 201;//websocket连接成功，且已经绑定了uid
    const CONNECT_WEBSOCKET_STATUS_ERROR = 202; //websocket连接成功但是 缺少参数

    #发送数据相关
    const SEND_STATUS_ERROR = 302; //发送数据过程当中出现了错误

    #接受数据相关
    const RECIVE_STATUS_SUCCESS = 400; //有新消息传输过来


    public static function onWorkerStart($businessWorker){
       //@todo maybe some coding....
    }

    //有连接进来时候获取生成的fd码然后发送给前台
    public static function onConnect($client_id){
        Gateway::sendToCurrentClient(sendWsToWeb(self::CONNECT_TCP_STATUS_SUCCESS,'TCP连接成功',['client_id'=>$client_id]));
    }

    //准备进行uid和client_id进行绑定
    public static function onWebSocketConnect($client_id, $data){
        //检测是否包含uid和type客户类型
        if (empty($data['get']['uid']) || empty($data['get']['type']) || ($data['get']['type'] != 1 && $data['get']['type'] != 2)) {
            Gateway::sendToCurrentClient(sendWsToWeb(self::CONNECT_WEBSOCKET_STATUS_ERROR,'缺少关键参数或者客户类型错误，断开连接',null));
            Gateway::closeClient($client_id);
        } else {
            //建立session关系和业务uid进行绑定
            Gateway::bindUid($client_id, $data['get']['uid']);
            //如果是客户则将客户放进一组中
            if ($data['get']['type'] == self::TYPE_CUSTOMER) {
                Gateway::joinGroup($client_id, self::TYPE_CUSTOMER); //加入客户组
            } else {
                Gateway::joinGroup($client_id, self::TYPE_SERVICER); //加入客服组
            }
            //记录缓存
            $_SESSION['uid'] = $data['get']['uid'];
            $_SESSION['type'] = $data['get']['type'];
            //绑定Uid
            Gateway::bindUid($client_id, $data['get']['uid']);
            Gateway::sendToCurrentClient(sendWsToWeb(self::CONNECT_WEBSOCKET_STATUS_SUCCESS,'websocket连接已经建立',null));
        }
    }

    //处理数据消息业务
    public static function onMessage($client_id, $message){

        $message_data = json_decode($message, true);
        //空数据不做响应
        if(!$message_data) return;
        //客户端回复心跳不做响应
        if ($message_data['type'] == 'pong') return;

        //处理消息
        if ($message_data['type'] == 'say') {
            if (empty($message_data['to_uid'])) {
                Gateway::sendToCurrentClient(sendWsToWeb(self::SEND_STATUS_ERROR,'缺少发送接受方uid', $message_data['message']));
            } else {

                if ($_SESSION['type'] == self::TYPE_CUSTOMER) {
                    //给管理员群组发送
                } elseif ($_SESSION['type'] == self::TYPE_SERVICER) {
                    //给单独客户发送并且判断是否在线，如果不在线 则直接进行数据库的存储
                    $is_online = Gateway::isUidOnline($message_data['to_uid']);
                    if ($is_online == self::OFFLINE_STATUS) {
                        //@todo 不在线时候进行数据库的存储,且标记为未读
                    } elseif($is_online == self::ONLINE_STATUS) {
                        //@todo 在线时候直接发送并且记录数据库且标记为已读
                        Gateway::sendToUid($message_data['to_uid'],sendWsToWeb(self::RECIVE_STATUS_SUCCESS,'接受到新数据',$message_data['message']));
                    }
                }
            }
        }
    }

    //处理客户端有退出
    public static function onClose($client_id){
        dump($client_id);
    }
}